package com.queue;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;

public class TestQueue {
    public  static void main(String []args)
    {
        final Object o=new Object();
        final BlockingQueue<String> queue=new ArrayBlockingQueue<String>(2);
        for(int i=0;i<2;i++)
        {
            new Thread(new Runnable() {
                public void run() {
                    while (true) {
                        try {
                            //Thread.sleep((int)Math.random()*10000000);
                            Thread.sleep(400);
                            System.out.println(Thread.currentThread().getName() + "准备生产汽车");
                            queue.put("qiche");
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                            System.out.println(Thread.currentThread().getName() + "生产汽车完毕，已有" + queue.size() + "辆汽车");

                    }


                }
            }).start();
        }

        new Thread(new Runnable() {
            public void run() {
                while (true) {
                        try {
                            //Thread.sleep((int)Math.random()*10000000);
                            Thread.sleep(1000);
                            System.out.println(Thread.currentThread().getName() + "准备提走汽车");

                            queue.take();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                        System.out.println(Thread.currentThread().getName() + "已经提走汽车,还有" + queue.size() + "辆汽车");
                    }


            }
        }).start();


    }
}
